package com.antdesignbackend.backend.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class SystemUtil {
    private static Logger logger = LoggerFactory.getLogger(SystemUtil.class);

    private SystemUtil() {
    }

    public static String getApplicationPath() {
        String appDir = System.getProperty("user.dir");
        return appDir;
    }

    public static int getOSType() {
        String name = System.getProperty("os.name");
        return name.startsWith("Windows") ? 1 : 2;
    }

    public static String getMacAddress() {
        String result = "";

        try {
            Enumeration netWorks = NetworkInterface.getNetworkInterfaces();

            while (true) {
                byte[] mac;
                do {
                    if (!netWorks.hasMoreElements()) {
                        return result;
                    }

                    NetworkInterface netWork = (NetworkInterface) netWorks.nextElement();
                    mac = netWork.getHardwareAddress();
                } while (mac == null);

                StringBuilder builder = new StringBuilder();

                for (int i = 0; i < mac.length; ++i) {
                    builder.append(String.format("%02X%s", mac[i], i < mac.length - 1 ? "-" : ""));
                }

                if (builder.length() > 0) {
                    result = builder.toString();
                    break;
                }
            }
        } catch (Exception var6) {
            logger.error("SystemUtil.getMacAddress()出错,原因是1.与网卡进行网络通信socket交互发生异常");
        }

        return result;
    }

    public static boolean startService(String serviceName) {
        return startService(serviceName, "UTF-8");
    }

    public static boolean startService(String serviceName, String charsetName) {
        boolean result = false;
        result = controlServer(serviceName, true);
        return result;
    }

    public static boolean stopService(String serviceName) {
        return stopService(serviceName, "UTF-8");
    }

    public static boolean stopService(String serviceName, String charsetName) {
        boolean result = false;
        result = controlServer(serviceName, false);
        return result;
    }

    private static boolean controlServer(String serviceName, boolean isStart) {
        return controlServer(serviceName, isStart, "UTF-8");
    }

    private static boolean controlServer(String serviceName, boolean isStart, String charsetName) {
        boolean result = false;
        BufferedReader reader = null;
        String command = "";
        if (isStart) {
            command = "net start " + serviceName;
        } else {
            command = "net stop " + serviceName;
        }

        Runtime runtime = Runtime.getRuntime();

        try {
            InputStream stream = runtime.exec(command).getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream, charsetName));

            String line;
            do {
                line = reader.readLine();
                if (line == null) {
                    throw new IOException("control service fail！");
                }
            } while (!line.contains("已经启动") && !line.contains("already") && !line.contains("成功") && !line.contains("success"));

            result = true;
        } catch (IOException var9) {
            logger.error("SystemUtil.controlServer(String serviceName, boolean isStart,String charsetName)报错,原因是1.命令执行失败2.IO读取失败");
        }

        return result;
    }
}
